import requests
import re
import os
from bs4 import BeautifulSoup
id=202422390507
def extract_url_split(style):
    start = style.find("url(") + 4
    end = style.find(")", start)
    return style[start:end].strip(" '\"")
def save_iamge(url_list,dirname):
    os.makedirs(dirname, exist_ok=True)
    for url in url_list:
        filename = os.path.basename(url)
        save_path = os.path.join(dirname, filename)
        response = requests.get(url, stream=True, timeout=10)
        response.raise_for_status()
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    f.write(chunk)
def extract_swiper_styles(html_content):
    """提取所有swiper-slide类的div样式"""
    soup = BeautifulSoup(html_content, 'html.parser')
    slides = soup.find_all('div', class_='swiper-slide')

    style_list = []
    for slide in slides:
        if 'style' in slide.attrs:
            # 标准化样式属性（去除多余空格和引号）
            style = slide['style'].strip().replace('"', '').replace("'", "")

            style_list.append('https://libapp.hueb.edu.cn/'+extract_url_split(style))
    return style_list
url = "https://libapp.hueb.edu.cn/UserBinding/photolist2.aspx"

headers = {
    # 基础请求头
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    # 用户代理（包含微信客户端信息）
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090c33) XWEB/11581 Flue",
    # 接受的内容类型
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    # 安全相关头
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Sec-Fetch-Dest": "document",
    # 引用来源
    "Referer": "https://libapp.hueb.edu.cn/UserBinding/index.aspx",
    # 接受编码和语言
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    # Cookie（包含会话和用户信息）
    "Cookie": f"ASP.NET_SessionId=wyzw0w5wnp2s0mq22w3dyaiz; UserOpenID=WechatTSG=odsLhshCyIPTt9jdPoQHcXuUPT3M; ReaderType=WechatTSG=%e8%80%83%e7%a0%94%e8%af%bb%e8%80%85; Reader_name=WechatTSG=%e4%ba%8e%e6%b4%aa%e6%9c%8b; Reader_BindStatus=WechatTSG=1; UserIdentID=WechatTSG={id}; ReaderNationalID=WechatTSG=130132200206194114"
}

try:
    response = requests.get(url, headers=headers)
    response.raise_for_status()  # 检查HTTP错误

    # 打印响应信息
    print("Status Code:", response.status_code)

    url_paths = extract_swiper_styles(response.text)
    print(url_paths)
    save_iamge(url_paths,str(id))
except requests.exceptions.RequestException as e:
    print("Request Failed:", e)